package dao.DAOImpl;

import bean.Customers;
import dao.CustomerDAO;
import utils.BaseDao;

import javax.swing.plaf.SliderUI;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author lucky monkey
 * @version 1.0
 * @date 2022-06-01 12:03
 */
public class CustomerDAOImpl extends BaseDao implements CustomerDAO {

    @Override
    public boolean insertCustomer(Connection connection, Customers customers) {
        String  sql="insert into  customers(name,email,birth) values(?,?,?)";
        boolean isSuccess= update(connection,sql,customers.getName(),customers.getEmail(),customers.getBirth());
        return isSuccess;
    }

    @Override
    public boolean deleteById(Connection connection, int id) {
        String sql="delete from customers where id=?";
        boolean update = update(connection, sql, id);
        return update;
    }

    @Override
    public boolean updateById(Connection connection, Customers customers) {
        String sql="update customers set name=?,email=?,birth=? where id=?";
        update(connection,sql,customers.getName(),customers.getEmail(),customers.getBirth(),customers.getId());
        return false;
    }

    @Override
    public Customers getCustomerById(Connection connection, int id) {
        String sql="select id,name,email,birth from customers where id=?";
        Customers instance = getInstance(connection, Customers.class, sql, id);
        return instance;
    }

    @Override
    public List<Customers> getAllCustomer(Connection connection) {
        String sql="select id,name,email,birth from customers";
        List<Customers> forList = getForList(connection, Customers.class, sql);
        return forList;
    }

    @Override
    public Long getCount(Connection connection) {
        String sql="select count(*) from customers";
        return getValue(connection, sql);
    }

    @Override
    public Date getMaxBirth(Connection connection) {
        String sql="select max(birth) from customers";
        return getValue(connection, sql);
    }
}
